package com.weiyuwqj.server.service.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.exception.HfException;
//import com.weiyuwqj.api.hengfeng.model.base.HfBaseResponseBean;
//import com.weiyuwqj.api.hengfeng.model.base.HfResultBean;
//import com.weiyuwqj.api.hengfeng.model.platform.HfPlatformCharge;
import com.weiyuwqj.api.util.StringUtils;
//import com.weiyuwqj.server.pay.PlatformApi;
import com.weiyuwqj.server.service.account.module.DealAction;
import com.weiyuwqj.server.service.account.module.DealModule;
import com.weiyuwqj.server.service.sys.module.ConfigTicketModule;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.server.mapper.*;
import com.weiyuwqj.server.service.account.module.action.RechargeDealAction;
import com.weiyuwqj.api.constant.Global;
import com.weiyuwqj.api.service.BusAccountRechargeService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.model.*;
import com.weiyuwqj.api.model.vo.BusAccountRechargeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


@Service("accountRechargeService")
public class BusAccountRechargeServiceImpl extends BaseServiceImpl<BusAccountRecharge> implements BusAccountRechargeService {

    @Autowired
    private BusAccountRechargeMapper busAccountRechargeMapper;

    @Autowired
    private DealModule dealModule;

    @Autowired
    private BusUserStatMapper userStatMapper;

    @Autowired
    private ConfigTicketModule ticketModule;

//    @Autowired
//    private PlatformApi platformApi;

    @Override
    public void updateBusAccountRechargeStatus(String orderNo, int oldStatus,
                                               int newStatus, String addIp) {
        BusAccountRecharge r = busAccountRechargeMapper.findBusAccountRechargeByOrderNo(orderNo);
        if (r.getStatus() != 2) {
            return;
        }
        //改状态
        int i = busAccountRechargeMapper.updateAccountRechargeStatus(orderNo, oldStatus, newStatus);
        if (i < 1) {
            return;
        }

        if (newStatus == 1) {
            //处理资金日志
            DealAction action = new RechargeDealAction();
            action.deal(r.getUserId(), r.getActualAmount(), "用戶充值:" + r.getActualAmount() + "元");
            int count = dealModule.dealAction(action);
            if (count < 1) {
                throw new CrowdException("update_error");
            }
            int userId = r.getUserId();
            Map<String, Object> map = new HashMap<String, Object>();
            //统计表格数据修改
            map.put("userId", userId);
            map.put("rechargeAmount", r.getRechargeAmount());
            userStatMapper.updateRecharge(map);

            //用户充值奖励
            TicketParamEntity rechargeParam = new TicketParamEntity();
            rechargeParam.setNid(Constants.recharge_nid);
            rechargeParam.setAmount(r.getActualAmount());
            rechargeParam.setUserId(userId);
            ticketModule.addTicket(rechargeParam);

        }

    }

    @Override
    public PageList getList(Paginator p, Map<String, Object> param) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<BusAccountRechargeVo> list = busAccountRechargeMapper.getList(param);
        return new PageList(list);
    }

    @Override
    public List<BusAccountRechargeVo> getList(Map<String, Object> param) {
        return busAccountRechargeMapper.getList(param);
    }

    @Override
    public BusAccountRechargeVo getSingle(Map<String, Object> param) {
        return busAccountRechargeMapper.getSingle(param);
    }

    //充值列表上方统计
    @Override
    public Map<String, Object> sumRechargeAmount(Map map) {
        return busAccountRechargeMapper.sumRechargeAmount(map);
    }

    @Override
    public List<BusAccountRechargeVo> excelList(Map<String, Object> param) {
        return busAccountRechargeMapper.getList(param);
    }

    //统计后台首页充值金额统计
    @Override
    public Map countRechargeAmount() {
        return busAccountRechargeMapper.countRechargeAmount();
    }

    @Override
    public BigDecimal sumRechargeAmountByUserId(Integer userId) {
        return busAccountRechargeMapper.sumRechargeAmountByUserId(userId);
    }

    @Override
    public PageList getListByUserId(Paginator p, Integer userId) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<BusAccountRechargeVo> list = busAccountRechargeMapper.getListByUserId(userId);
        return new PageList(list);
    }

    @Override
    public PageList getDailyList(Paginator p, Map<String, Object> map) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<BusAccountRechargeVo> list = busAccountRechargeMapper.getDailyList(map);
        return new PageList(list);
    }

    //后台首页趋势图
    @Override
    public List<Map> getRegionGroupCountRechargeList(Map map) {
        return busAccountRechargeMapper.getRegionGroupCountRechargeList(map);
    }

    @Override
    public PageList listByGuaranteeId(Paginator p, Map<String, Object> map) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<BusAccountRechargeVo> list = busAccountRechargeMapper.listByGuaranteeId(map);
        return new PageList(list);
    }

//    /**
//     * 平台充值
//     *
//     * @param amount
//     * @param resource
//     * @param addip
//     * @param remark
//     * @param type
//     * @return map
//     */
//    @Override
//    public Map platformRecharge(String amount,
//                                String resource,
//                                String addip,
//                                String remark,
//                                Integer type) {
//        //插入平台充值记录
//        String orderNo = StringUtils.getOrderNo();
//        BusAccountRecharge recharge = new BusAccountRecharge();
//        recharge.setUserId(Integer.parseInt(Global.getOwnChildAccountId()));
//        recharge.setResource(resource);
//        recharge.setRemark(remark);
//        recharge.setAddip(addip);
//        recharge.setAddtime(new Date());
//        recharge.setFeeAmount(BigDecimal.ZERO);
//        //recharge.setFeeRate();
//        recharge.setActualAmount(new BigDecimal(amount));
//        recharge.setRechargeAmount(new BigDecimal(amount));
//        recharge.setStatus(2);
//        recharge.setType(type);
//        recharge.setOrderNo(orderNo);
//        busAccountRechargeMapper.insert(recharge);
//
//        //请求恒丰
//        HfPlatformCharge platformCharge = new HfPlatformCharge();
//        platformCharge.setAmount(amount);
//        platformCharge.setNotify_url(Global.getPlatformChargeNoticeUrl());
//        platformCharge.setOrder_no(orderNo);
//        platformCharge.setRemark(remark);
//	    HfResultBean result = platformApi.doPlatformCharge(platformCharge); // 请求恒丰
//	    return Message.OK;
//    }

    @Override
    public int insertRecharge(BusAccountRecharge recharge) {
        return busAccountRechargeMapper.insertRecharge(recharge);
    }

}
